import sys
from PyQt5.QtCore import Qt, QTimer, QDateTime, QPointF ,QRectF
from PyQt5.QtGui import QPainter, QPen, QBrush, QFont, QPolygonF, QColor,QPixmap
from PyQt5.QtWidgets import QMainWindow, QPushButton, QLabel, QApplication

class Me(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        # 初始化成员变量
        self.mouseX = 0
        self.mouseY = 0
        self.Ele = 0
        self.pub_ZDA = 0
        self.pub_ZFA = 0
        self.pub_ZQA = 0
        self.pub_ZRA = 0
        self.pub_CA_1_3 = 0
        self.pub_CA_2_4 = 0
        self.pub_CA_5_7 = 0
        self.pub_CA_6_8 = 0
        self.pub_CA_9 = 0
        self.pub_CA_10 = 0
        self.pub_CA_11 = 0
        self.pub_CA_12 = 0
        self.pub_X = 0
        self.pub_XF = 0
        self.pub_X4 = 0
        self.pub_S = 0
        self.pub_SI = 0
        self.pub_S3 = 0
        self.pub_SII = 0
        self.pub_S4 = 0
        self.pub_D5 = 0
        self.pub_D6 = 0
        self.pub_XII = 0
        self.pub_XIIS = 0
        self.pub_SXII = 0
        self.pub_X3 = 0
        self.pub_SX3 = 0
        self.pub_X4S = 0
        self.pub_SX4 = 0
        self.pub_X1S = 0
        self.pub_SX1 = 0
        self.pub_QD5_9GZ1 = 0
        self.pub_QD5_9GZ0 = 0
        self.pub_QD5_9GZJS = 0
        self.ZSXSI = 0
        self.ZSXS3 = 0
        self.ZSXSII = 0
        self.ZSXS4 = 0
        self.ZSD5SI = 0
        self.ZSD6XII = 0
        self.ZSS4X = 0
        self.ZSXI_SF = 0
        self.ZSXF_SII = 0
        self.ZSXF_S4 = 0
        self.ZSXII_S = 0
        self.ZSX4_S = 0
        self.ZSX3_SF = 0  # X3->D8->D4->D2->SF路径状态
        self.ZSD1D3 = 0
        self.ZSD3D7 = 0
        self.ZSD7SII = 0
        self.ZSD7S4 = 0

        self.pub_XC=0
        self.pub_SCLC = 0

        self.section = 0; self.section1 = 0; self.section2 = 0; self.section3 = 0

        # 清理旧的状态变量，使用新的单一状态变量
        self.XS3section_state = 0  # 0=未运行, 1=第一段, 2=转弯段, 3=上行线段, 4=完成

        self.D5SIsection = 0

        self.pub_XI = 0; self.pub_SF = 0
        self.XISFsection_state = 0
        self.dc_1_3_locked = False
        self.dc_1_3_pos = 0

        # 为其它道岔添加状态
        self.dc_5_7_locked = False
        self.dc_5_7_pos = 0
        self.dc_9_locked = False
        self.dc_9_pos = 0
        self.dc_11_locked = False
        self.dc_11_pos = 0


        self.setWindowTitle("车站信号控制系统软件模拟")
        self.setFixedSize(1908, 815)

        self.Tc = QLabel(self)
        self.Tc.setStyleSheet("background-color:rgb(0,0,0); font-size:25px; color:white")

        self.Text = QLabel(self)
        self.Text.setFont(QFont("宋体", 11))
        self.Text.setText("状态：正在加载...")
        self.Text.move(100, 100)
        self.Text.adjustSize()
        self.Text.hide()

        self.trainLabel = QLabel(self)
        pix = QPixmap(r"E:\\A\\大学\\大三下\\交通软件\\interlocking_simulator\\50b73c523b8313eda39b5d9b1efc4f5.png")
        scaled_pixmap = pix.scaled(100, 50, Qt.KeepAspectRatio, Qt.SmoothTransformation)
        self.trainLabel.setPixmap(scaled_pixmap)
        self.trainLabel.hide()


        self._create_buttons()

        self.Tclock = QTimer(self)
        self.Tclock.timeout.connect(self.Time_Display)
        self.Tclock.start(1000)

    def _create_buttons(self):
        self.Power_on = QPushButton("模拟通电", self); self.Power_on.move(843, 655); self.Power_on.adjustSize()
        self.Power_off = QPushButton("模拟断电", self); self.Power_off.move(940, 655); self.Power_off.adjustSize()
        self.CA_1_3 = QPushButton("1/3", self); self.CA_1_3.setGeometry(268, 42, 35, 25)
        self.CA_5_7 = QPushButton("5/7", self); self.CA_5_7.setGeometry(343, 42, 35, 25)
        self.CA_9 = QPushButton("9", self); self.CA_9.setGeometry(418, 42, 35, 25)
        self.CA_11 = QPushButton("11", self); self.CA_11.setGeometry(493, 42, 35, 25)
        self.CA_2_4 = QPushButton("2/4", self); self.CA_2_4.setGeometry(1394, 42, 35, 25)
        self.CA_6_8 = QPushButton("6/8", self); self.CA_6_8.setGeometry(1469, 42, 35, 25)
        self.CA_10 = QPushButton("10", self); self.CA_10.setGeometry(1544, 42, 35, 25)
        self.CA_12 = QPushButton("12", self); self.CA_12.setGeometry(1619, 42, 35, 25)
        self.ZDA = QPushButton("总定位", self); self.ZDA.move(131, 735); self.ZDA.adjustSize()
        self.ZFA = QPushButton("总反位", self); self.ZFA.move(231, 735); self.ZFA.adjustSize()
        self.ZQA = QPushButton("总取消", self); self.ZQA.move(331, 735); self.ZQA.adjustSize()
        self.ZRA = QPushButton("总人解", self); self.ZRA.move(431, 735); self.ZRA.adjustSize()
        self.XC = QPushButton("模拟行车", self); self.XC.move(531, 735); self.XC.adjustSize()
        self.SCLC = QPushButton("删除列车", self); self.SCLC.move(631, 735); self.SCLC.adjustSize()
        self.S3 = QPushButton("S3", self); self.S3.setGeometry(688, 225, 35, 35)
        self.X3 = QPushButton("X3", self); self.X3.setGeometry(947, 176, 35, 35)
        self.SI = QPushButton("SI", self); self.SI.setGeometry(648, 349, 35, 35)
        self.XI = QPushButton("XI", self); self.XI.setGeometry(962, 300, 35, 35)
        self.SII = QPushButton("SII", self); self.SII.setGeometry(648, 460, 35, 35)
        self.XII = QPushButton("XII", self); self.XII.setGeometry(995, 411, 35, 35)
        self.S4 = QPushButton("S4", self); self.S4.setGeometry(673, 562, 35, 35)
        self.X4 = QPushButton("X4", self); self.X4.setGeometry(991, 513, 35, 35)
        self.X = QPushButton("X", self); self.X.setGeometry(26, 301, 35, 35)
        self.XF = QPushButton("XF", self); self.XF.setGeometry(26, 456, 35, 35)
        self.D1 = QPushButton("D1", self); self.D1.setGeometry(141, 408, 35, 35)
        self.D3 = QPushButton("D3", self); self.D3.setGeometry(329, 458, 35, 35)
        self.D5 = QPushButton("D5", self); self.D5.setGeometry(321, 304, 35, 35)
        self.D7 = QPushButton("D7", self); self.D7.setGeometry(473, 414, 25, 25)
        self.SF = QPushButton("SF", self); self.SF.setGeometry(1804, 305, 35, 35)
        self.S = QPushButton("S", self); self.S.setGeometry(1797, 456, 35, 35)
        self.D2 = QPushButton("D2", self); self.D2.setGeometry(1695, 347, 35, 35)
        self.D4 = QPushButton("D4", self); self.D4.setGeometry(1471, 301, 35, 35)
        self.D6 = QPushButton("D6", self); self.D6.setGeometry(1492, 459, 35, 35)
        self.D8 = QPushButton("D8", self); self.D8.setGeometry(1263, 351, 35, 35)

        self.Power_on.clicked.connect(self.Btn_Power_on)
        self.Power_off.clicked.connect(self.Btn_Power_off)
        self.ZQA.clicked.connect(self.Btn_ZQA)
        self.ZRA.clicked.connect(self.Btn_ZRA)
        self.ZFA.clicked.connect(self.Btn_ZFA)
        self.ZDA.clicked.connect(self.Btn_ZDA)
        self.XC.clicked.connect(self.Btn_XC)
        self.SCLC.clicked.connect(self.Btn_SCLC)
        self.CA_1_3.clicked.connect(self.Btn_CA_1_3)
        self.CA_5_7.clicked.connect(self.Btn_CA_5_7)
        self.CA_9.clicked.connect(self.Btn_CA_9)
        self.X.clicked.connect(self.Btn_X)
        self.SI.clicked.connect(self.Btn_SI)
        self.S3.clicked.connect(self.Btn_S3)
        self.SII.clicked.connect(self.Btn_SII)
        self.S4.clicked.connect(self.Btn_S4)
        self.CA_2_4.clicked.connect(self.Btn_CA_2_4)
        self.D5.clicked.connect(self.Btn_D5)
        self.D6.clicked.connect(self.Btn_D6)
        self.XII.clicked.connect(self.Btn_XII)
        self.XI.clicked.connect(self.Btn_XI)
        self.SF.clicked.connect(self.Btn_SF)
        self.CA_9.clicked.connect(self.Btn_CA_9)
        self.CA_11.clicked.connect(self.Btn_CA_11)
        self.XF.clicked.connect(self.Btn_XF)
        self.S.clicked.connect(self.Btn_S)
        self.X4.clicked.connect(self.Btn_X4)
        self.X3.clicked.connect(self.Btn_X3)

    def Btn_XI(self):
        self.pub_XI = 1
        if self.pub_SF == 1: self.ZSXI_SF = 1
        self.update()

    def Btn_SF(self):
        self.pub_SF = 1
        if self.pub_XI == 1: 
            self.ZSXI_SF = 1
            self.Text.setText(f"XI-SF路径已建立: XI={self.pub_XI}, SF={self.pub_SF}, Ele={self.Ele}")
        elif self.pub_X3 == 1 and self.Ele == 1:
            self.ZSX3_SF = 1
            self.Text.setText(f"X3-SF路径已建立: X3={self.pub_X3}, SF={self.pub_SF}, Ele={self.Ele}")
        else:
            self.Text.setText(f"SF按钮已按下: XI={self.pub_XI}, X3={self.pub_X3}, SF={self.pub_SF}, Ele={self.Ele}")
        self.Text.adjustSize(); self.Text.show()
        self.update()

    def Btn_Power_on(self): self.Ele = 1; self.update()
    def Btn_Power_off(self): self.Ele = 0; self.update()

    def Btn_ZQA(self):
        self.ZSXSI = 0; self.ZSXS3 = 0; self.ZSXI_SF = 0; self.ZSD5SI = 0
        self.pub_X = 0; self.pub_SI = 0; self.pub_S3 = 0; self.pub_XI = 0; self.pub_SF = 0; self.pub_D5 = 0
        self.Text.setText("所有未锁闭进路已取消"); self.Text.adjustSize(); self.Text.show()
        self.update(); QTimer.singleShot(3000, self.clearText)

    def Btn_ZRA(self):
        self.Text.setText("总人解已触发，3秒后执行解锁..."); self.Text.adjustSize(); self.Text.show()
        QTimer.singleShot(3000, self.execute_unlock_all_routes)

    def execute_unlock_all_routes(self):
        self.ZSXSI = 0; self.ZSXS3 = 0; self.ZSXI_SF = 0; self.ZSD5SI = 0
        self.pub_X = 0; self.pub_SI = 0; self.pub_S3 = 0; self.pub_XI = 0; self.pub_SF = 0; self.pub_D5 = 0
        self.Text.setText("所有进路已人工解锁"); self.Text.adjustSize(); self.Text.show()
        self.update(); QTimer.singleShot(2000, self.clearText)


    def Btn_ZFA(self):
        # 总反位：所有未封锁道岔反位
        switches = {'1/3': 'dc_1_3', '5/7': 'dc_5_7', '9': 'dc_9', '11': 'dc_11'}
        operated_switches = []
        locked_switches = []

        for name, prefix in switches.items():
            is_locked = getattr(self, f"{prefix}_locked", False)
            if not is_locked:
                setattr(self, f"{prefix}_pos", 2)  # 2=反位
                operated_switches.append(name)
            else:
                locked_switches.append(name)

        message = []
        if operated_switches:
            message.append(f"道岔 {', '.join(operated_switches)} 已操作至反位。")
        if locked_switches:
            message.append(f"道岔 {', '.join(locked_switches)} 已封锁，操作跳过。")

        if not message:
            message.append("没有可操作的道岔。")

        self.Text.setText("\n".join(message))
        self.Text.adjustSize(); self.Text.show()
        self.update()
        QTimer.singleShot(3000, self.DCqing)

    def Btn_ZDA(self):
        # 总定位：所有未封锁道岔定位
        switches = {'1/3': 'dc_1_3', '5/7': 'dc_5_7', '9': 'dc_9', '11': 'dc_11'}
        operated_switches = []
        locked_switches = []

        for name, prefix in switches.items():
            is_locked = getattr(self, f"{prefix}_locked", False)
            if not is_locked:
                setattr(self, f"{prefix}_pos", 1)  # 1=定位
                operated_switches.append(name)
            else:
                locked_switches.append(name)

        message = []
        if operated_switches:
            message.append(f"道岔 {', '.join(operated_switches)} 已操作至定位。")
        if locked_switches:
            message.append(f"道岔 {', '.join(locked_switches)} 已封锁，操作跳过。")

        if not message:
            message.append("没有可操作的道岔。")

        self.Text.setText("\n".join(message))
        self.Text.adjustSize(); self.Text.show()
        self.update()
        QTimer.singleShot(3000, self.DCqing)


    def Btn_XC(self):
        self.pub_XC = 1
        if self.ZSXSI == 1:
            self.start_XSI_train()
        elif self.ZSXS3 == 1:
            self.start_XS3_train()
        elif self.ZSD5SI == 1:
            self.start_D5_SI_train()
        elif self.ZSXI_SF == 1:
            self.start_XI_SF_train()
        elif self.ZSXF_SII == 1:
            self.start_XF_SII_train()
        elif self.ZSXF_S4 == 1:
            self.start_XF_S4_train()
        elif self.ZSXII_S == 1:
            self.start_XII_S_train()
        elif self.ZSX4_S == 1:
            self.start_X4_S_train()
        elif self.ZSX3_SF == 1:
            self.start_X3_SF_train()
        self.update()

    def Btn_SCLC(self):
        self.trainLabel.hide()
        if hasattr(self, 'moveTimer'): self.moveTimer.stop()
        if hasattr(self, 'moveTimer_XISF'): self.moveTimer_XISF.stop()

    def start_XSI_train(self):
        path_points = [QPointF(65, 312), QPointF(361, 312), QPointF(645, 312), QPointF(1000, 312), QPointF(1050, 312)]
        steps = [50, 50, 50, 10]
        self.trainPath = []
        for i in range(len(path_points) - 1):
            self.interpolate_path(path_points[i], path_points[i+1], steps[i])
        self.trainIndex = 0; self.section = 0
        self.trainLabel.move(*self.trainPath[0]); self.trainLabel.show()
        self.moveTimer = QTimer(self)
        self.moveTimer.timeout.connect(self._move_train_stepXSI)
        self.moveTimer.start(50)

    def start_XS3_train(self):
        path_points = [QPointF(65, 312), QPointF(361, 312), QPointF(429, 312), QPointF(625, 188), QPointF(678, 188), QPointF(985, 188), QPointF(1008, 188)]
        steps = [50, 20, 50, 20, 50, 10]
        self.trainPath = []
        for i in range(len(path_points) - 1):
            self.interpolate_path(path_points[i], path_points[i+1], steps[i])
        self.trainIndex = 0; self.XS3section_state = 0
        self.trainLabel.move(*self.trainPath[0]); self.trainLabel.show()
        self.moveTimer = QTimer(self)
        self.moveTimer.timeout.connect(self._move_train_stepXS3)
        self.moveTimer.start(50)

    def start_D5_SI_train(self):
        self.trainPath = []
        self.interpolate_path(QPointF(363, 342), QPointF(627, 342), 50)
        self.trainIndex = 0; self.trainLabel.move(*self.trainPath[0]); self.trainLabel.show()
        self.moveTimer = QTimer(self); self.moveTimer.timeout.connect(self._move_train_step_D5_SI); self.moveTimer.start(50)

    def start_XI_SF_train(self):
        self.trainPath_XISF = []
        self.interpolate_path(QPointF(1010, 312), QPointF(1400, 312), 50, is_xisf=True)
        self.interpolate_path(QPointF(1400, 312), QPointF(1820, 312), 50, is_xisf=True)
        self.trainIndex_XISF = 0; self.trainLabel.move(*self.trainPath_XISF[0]); self.trainLabel.show()
        self.moveTimer_XISF = QTimer(self); self.moveTimer_XISF.timeout.connect(self._move_train_step_XI_SF); self.moveTimer_XISF.start(50)

    def interpolate_path(self, p1, p2, steps, is_xisf=False):
        path = self.trainPath_XISF if is_xisf else self.trainPath
        for i in range(steps):
            t = i / float(steps)
            path.append((int(p1.x() * (1 - t) + p2.x() * t), int(p1.y() * (1 - t) + p2.y() * t)))

    def _move_train_step_D5_SI(self):
        self.trainIndex += 1
        if self.trainIndex >= len(self.trainPath):
            self.moveTimer.stop(); self.D5SIsection = 0; self.pub_D5 = 0; self.pub_SI = 0; self.ZSD5SI = 0; return
        x, y = self.trainPath[self.trainIndex]; self.trainLabel.move(x, y)
        self.D5SIsection = 1 if 363 <= x <= 627 and abs(y - 342) < 5 else 0; self.update()

    def _move_train_step_XI_SF(self):
        self.trainIndex_XISF += 1
        if self.trainIndex_XISF >= len(self.trainPath_XISF):
            self.moveTimer_XISF.stop(); self.pub_XI = 0; self.pub_SF = 0; self.ZSXI_SF = 0; self.XISFsection_state = 4; return
        x, y = self.trainPath_XISF[self.trainIndex_XISF]; self.trainLabel.move(x, y)
        if 1010 <= x < 1200: self.XISFsection_state = 1
        elif 1200 <= x < 1600: self.XISFsection_state = 2
        elif 1600 <= x <= 1820: self.XISFsection_state = 3
        self.update()

    def _move_train_stepXS3(self):
        self.trainIndex += 1
        if self.trainIndex >= len(self.trainPath): self.moveTimer.stop(); self.XC_XS3qing(); return
        x, y = self.trainPath[self.trainIndex]; self.trainLabel.move(x, y)
        if x < 429: self.XS3section_state = 1
        elif x < 625: self.XS3section_state = 2
        elif x < 985: self.XS3section_state = 3
        else: self.XS3section_state = 4
        self.update()

    def _move_train_stepXSI(self):
        self.trainIndex += 1
        if self.trainIndex >= len(self.trainPath): self.moveTimer.stop(); self.XC_XSIqing(); return
        x, y = self.trainPath[self.trainIndex]; self.trainLabel.move(x, y)
        if x < 361: self.section = 1
        elif x < 645: self.section1 = 2
        elif x < 1000: self.section2 = 3
        elif x < 1258: self.section3 = 4
        self.update()

    def XC_XSIqing(self):
        self.section=0; self.section1=0; self.section2=0; self.section3=0
        self.pub_X=0; self.pub_SI=0; self.ZSXSI = 0; self.update()

    def XC_XS3qing(self):
        self.pub_X = 0; self.pub_S3 = 0; self.ZSXS3 = 0; self.XS3section_state = 0; self.update()

    def start_XF_SII_train(self):
        # 路径建立时XF信号灯变黄
        self.xf_signal_color = "yellow"
        
        path_points = [
            QPointF(26, 456),  # XF
            QPointF(141, 456),  # 区段1
            QPointF(329, 456),  # 区段2
            QPointF(473, 456),  # 区段3
            QPointF(648, 456),  # 区段4
            QPointF(700, 456)   # SII
        ]
        steps = [50, 50, 50, 50, 10]
        self.trainPath = []
        for i in range(len(path_points) - 1):
            self.interpolate_path(path_points[i], path_points[i+1], steps[i])
            
        self.XFSII_section_state = 0  # 区段状态
        self.trainIndex = 0
        self.trainLabel.move(*self.trainPath[0]); self.trainLabel.show()
        self.moveTimer = QTimer(self)
        self.moveTimer.timeout.connect(self._move_train_step_XF_SII)
        self.moveTimer.start(50)

    def _move_train_step_XF_SII(self):
        self.trainIndex += 1
        if self.trainIndex >= len(self.trainPath):
            self.moveTimer.stop(); self.XC_XF_SIIqing(); return
            
        x, y = self.trainPath[self.trainIndex]
        self.trainLabel.move(x, y)
        
        # 更新区段占用状态
        if x < 141 and self.XFSII_section_state < 1:
            self.XFSII_section_state = 1
        elif 141 <= x < 329 and self.XFSII_section_state < 2:
            self.XFSII_section_state = 2
        elif 329 <= x < 473 and self.XFSII_section_state < 3:
            self.XFSII_section_state = 3
        elif 473 <= x < 648 and self.XFSII_section_state < 4:
            self.XFSII_section_state = 4
        elif x >= 648 and self.XFSII_section_state < 5:
            self.XFSII_section_state = 5
            
        self.update()

    def XC_XF_SIIqing(self):
        self.pub_XF = 0
        self.pub_SII = 0 
        self.ZSXF_SII = 0
        self.xf_signal_color = "red"  # 恢复信号灯
        self.XFSII_section_state = 0   # 重置区段状态
        self.update()
        QTimer.singleShot(500, lambda: self.update())  # 强制刷新界面

    def start_XF_S4_train(self):
        # 路径建立时XF信号灯变黄
        self.xf_signal_color = "yellow"
        
        path_points = [
            QPointF(26, 456),  # XF
            QPointF(141, 456),  # 区段1
            QPointF(329, 456),  # 区段2
            QPointF(473, 456),  # 区段3
            QPointF(673, 562),  # 区段4
            QPointF(700, 562)   # S4
        ]
        steps = [50, 50, 50, 50, 10]
        self.trainPath = []
        for i in range(len(path_points) - 1):
            self.interpolate_path(path_points[i], path_points[i+1], steps[i])
            
        self.XFS4_section_state = 0  # 区段状态
        self.trainIndex = 0
        self.trainLabel.move(*self.trainPath[0]); self.trainLabel.show()
        self.moveTimer = QTimer(self)
        self.moveTimer.timeout.connect(self._move_train_step_XF_S4)
        self.moveTimer.start(50)

    def _move_train_step_XF_S4(self):
        self.trainIndex += 1
        if self.trainIndex >= len(self.trainPath):
            self.moveTimer.stop(); self.XC_XF_S4qing(); return
            
        x, y = self.trainPath[self.trainIndex]
        self.trainLabel.move(x, y)
        
        # 更新区段占用状态
        if x < 141 and self.XFS4_section_state < 1:
            self.XFS4_section_state = 1
        elif 141 <= x < 329 and self.XFS4_section_state < 2:
            self.XFS4_section_state = 2
        elif 329 <= x < 473 and self.XFS4_section_state < 3:
            self.XFS4_section_state = 3
        elif 473 <= x < 673 and self.XFS4_section_state < 4:
            self.XFS4_section_state = 4
        elif x >= 673 and self.XFS4_section_state < 5:
            self.XFS4_section_state = 5
            
        self.update()

    def XC_XF_S4qing(self):
        self.pub_XF = 0
        self.pub_S4 = 0 
        self.ZSXF_S4 = 0
        self.xf_signal_color = "red"  # 恢复信号灯
        self.XFS4_section_state = 0   # 重置区段状态
        self.update()
        QTimer.singleShot(500, lambda: self.update())  # 强制刷新界面

    def start_XII_S_train(self):
        # 路径建立时XII信号灯变黄
        self.xii_signal_color = "yellow"
        
        path_points = [
            QPointF(995, 411),  # XII
            QPointF(1492, 411),  # 区段1
            QPointF(1797, 411),  # 区段2
            QPointF(1820, 411)   # S
        ]
        steps = [50, 50, 10]
        self.trainPath = []
        for i in range(len(path_points) - 1):
            self.interpolate_path(path_points[i], path_points[i+1], steps[i])
            
        self.XIIS_section_state = 0  # 区段状态
        self.trainIndex = 0
        self.trainLabel.move(*self.trainPath[0]); self.trainLabel.show()
        self.moveTimer = QTimer(self)
        self.moveTimer.timeout.connect(self._move_train_step_XII_S)
        self.moveTimer.start(50)

    def _move_train_step_XII_S(self):
        self.trainIndex += 1
        if self.trainIndex >= len(self.trainPath):
            self.moveTimer.stop(); self.XC_XII_Sqing(); return
            
        x, y = self.trainPath[self.trainIndex]
        self.trainLabel.move(x, y)
        
        # 更新区段占用状态
        if x < 1492 and self.XIIS_section_state < 1:
            self.XIIS_section_state = 1
        elif 1492 <= x < 1797 and self.XIIS_section_state < 2:
            self.XIIS_section_state = 2
        elif x >= 1797 and self.XIIS_section_state < 3:
            self.XIIS_section_state = 3
            
        self.update()

    def XC_XII_Sqing(self):
        self.pub_XII = 0
        self.pub_S = 0 
        self.ZSXII_S = 0
        self.xii_signal_color = "red"  # 恢复信号灯
        self.XIIS_section_state = 0   # 重置区段状态
        self.update()
        QTimer.singleShot(500, lambda: self.update())  # 强制刷新界面

    def start_X4_S_train(self):
        # 路径建立时X4信号灯变黄
        self.x4_signal_color = "yellow"
        
        # 使用与paintEvent中完全一致的路径点
        path_points = [
            QPointF(991, 553),  # X4
            QPointF(1140, 555),  # 区段1
            QPointF(1280, 453),  # 区段2
            QPointF(1492, 453),  # 区段3
            QPointF(1797, 453)   # S
        ]
        steps = [10] * (len(path_points)-1)  # 均匀分配步数
        self.trainPath = []
        for i in range(len(path_points) - 1):
            self.interpolate_path(path_points[i], path_points[i+1], steps[i])
            
        self.X4S_section_state = 0  # 区段状态
        self.trainIndex = 0
        self.trainLabel.move(*self.trainPath[0]); self.trainLabel.show()
        self.moveTimer = QTimer(self)
        self.moveTimer.timeout.connect(self._move_train_step_X4_S)
        self.moveTimer.start(50)

    def _move_train_step_X4_S(self):
        self.trainIndex += 1
        if self.trainIndex >= len(self.trainPath):
            self.moveTimer.stop(); self.XC_X4_Sqing(); return
            
        x, y = self.trainPath[self.trainIndex]
        self.trainLabel.move(x, y)
        
        # 更新区段占用状态
        if x < 1140 and self.X4S_section_state < 1:
            self.X4S_section_state = 1
        elif 1140 <= x < 1280 and self.X4S_section_state < 2:
            self.X4S_section_state = 2
        elif 1280 <= x < 1492 and self.X4S_section_state < 3:
            self.X4S_section_state = 3
        elif x >= 1492 and self.X4S_section_state < 4:
            self.X4S_section_state = 4
            
        self.update()

    def XC_X4_Sqing(self):
        self.pub_X4 = 0
        self.pub_S = 0 
        self.ZSX4_S = 0
        self.x4_signal_color = "red"  # 恢复信号灯
        self.X4S_section_state = 0   # 重置区段状态
        self.update()
        QTimer.singleShot(500, lambda: self.update())  # 强制刷新界面

    def start_X3_SF_train(self):
        # 路径建立时X3信号灯变黄
        self.x3_signal_color = "yellow"
        
        path_points = [
            QPointF(947, 196),  # X3
            QPointF(1000, 196),  # 区段1
            QPointF(1200, 345),  # 区段2 
            QPointF(1263, 345),  # D8
            QPointF(1471, 345),  # D4
            QPointF(1695, 345),  # D2
            QPointF(1804, 305)   # SF
        ]
        steps = [20, 20, 20, 20, 20, 10]
        self.trainPath = []
        for i in range(len(path_points) - 1):
            self.interpolate_path(path_points[i], path_points[i+1], steps[i])
            
        self.X3SF_section_state = 0  # 区段状态
        self.trainIndex = 0
        self.trainLabel.move(*self.trainPath[0]); self.trainLabel.show()
        self.moveTimer = QTimer(self)
        self.moveTimer.timeout.connect(self._move_train_step_X3_SF)
        self.moveTimer.start(50)

    def _move_train_step_X3_SF(self):
        self.trainIndex += 1
        if self.trainIndex >= len(self.trainPath):
            self.moveTimer.stop(); self.XC_X3_SFqing(); return
            
        x, y = self.trainPath[self.trainIndex]
        self.trainLabel.move(x, y)
        
        # 更新区段占用状态
        if x < 1000 and self.X3SF_section_state < 1:
            self.X3SF_section_state = 1
        elif 1000 <= x < 1263 and self.X3SF_section_state < 2:
            self.X3SF_section_state = 2 
        elif 1263 <= x < 1471 and self.X3SF_section_state < 3:
            self.X3SF_section_state = 3
        elif 1471 <= x < 1695 and self.X3SF_section_state < 4:
            self.X3SF_section_state = 4
        elif x >= 1695 and self.X3SF_section_state < 5:
            self.X3SF_section_state = 5
            
        self.update()

    def XC_X3_SFqing(self):
        self.pub_X3 = 0
        self.pub_SF = 0 
        self.ZSX3_SF = 0
        self.x3_signal_color = "red"  # 恢复信号灯
        self.X3SF_section_state = 0   # 重置区段状态
        self.update()
        QTimer.singleShot(500, lambda: self.update())  # 强制刷新界面

    def Btn_CA_1_3(self):
        self.dc_1_3_locked = not self.dc_1_3_locked
        text = "1/3道岔已封锁" if self.dc_1_3_locked else "1/3道岔已解锁"
        self.Text.setText(text); self.Text.adjustSize(); self.Text.show(); self.update()

    def Btn_CA_5_7(self):
        self.dc_5_7_locked = not self.dc_5_7_locked
        text = "5/7道岔已封锁" if self.dc_5_7_locked else "5/7道岔已解锁"
        self.Text.setText(text); self.Text.adjustSize(); self.Text.show(); self.update()

    def Btn_CA_9(self):
        self.dc_9_locked = not self.dc_9_locked
        text = "9道岔已封锁" if self.dc_9_locked else "9道岔已解锁"
        self.Text.setText(text); self.Text.adjustSize(); self.Text.show(); self.update()

    def Btn_CA_11(self):
        self.dc_11_locked = not self.dc_11_locked
        text = "11道岔已封锁" if self.dc_11_locked else "11道岔已解锁"
        self.Text.setText(text); self.Text.adjustSize(); self.Text.show(); self.update()

    def Btn_X(self):
        if self.dc_1_3_locked: self.Text.setText("1/3道岔封锁，进路无法建立"); self.Text.adjustSize(); self.Text.show(); return
        self.pub_X = 1
        if self.pub_SI == 1: self.ZSXSI = 1
        if self.pub_S3 == 1: self.ZSXS3 = 1
        self.update()

    def clearText(self): self.Text.hide()

    def DCqing(self):
        self.dc_1_3_pos = 0 # 恢复道岔显示为默认
        self.dc_5_7_pos = 0
        self.dc_9_pos = 0
        self.dc_11_pos = 0
        self.update(); QTimer.singleShot(100, self.clearText)

    def Btn_SI(self):
        if self.dc_1_3_locked: self.Text.setText("1/3道岔封锁，进路无法建立"); self.Text.adjustSize(); self.Text.show(); return
        self.pub_SI = 1
        if self.pub_X == 1: self.ZSXSI = 1
        if self.pub_D5 == 1: self.ZSD5SI = 1
        self.update()

    def Btn_S3(self):
        self.pub_S3 = 1
        if self.pub_X == 1: self.ZSXS3 = 1
        self.update()

    def Btn_SII(self): 
        self.pub_SII = 1
        if self.pub_XF == 1 and self.Ele == 1: 
            self.ZSXF_SII = 1
            self.Text.setText(f"XF-SII路径已建立: XF={self.pub_XF}, SII={self.pub_SII}, Ele={self.Ele}")
        else:
            self.Text.setText(f"XF-SII路径条件不满足: XF={self.pub_XF}, SII={self.pub_SII}, Ele={self.Ele}")
        self.Text.adjustSize(); self.Text.show()
        self.update()
    def Btn_S4(self): 
        self.pub_S4 = 1
        if self.pub_XF == 1 and self.Ele == 1: 
            self.ZSXF_S4 = 1
            self.Text.setText(f"XF-S4路径已建立: XF={self.pub_XF}, S4={self.pub_S4}, Ele={self.Ele}")
        else:
            self.Text.setText(f"XF-S4路径条件不满足: XF={self.pub_XF}, S4={self.pub_S4}, Ele={self.Ele}")
        self.Text.adjustSize(); self.Text.show()
        self.update()
    def Btn_CA_2_4(self): self.pub_CA_2_4 = 1; self.update()

    def Btn_D5(self):
        self.pub_D5 = 1
        if self.pub_SI == 1: self.ZSD5SI = 1
        self.update()

    def Btn_D6(self): self.pub_D6 = 1; self.update()
    
    def Btn_X3(self):
        self.pub_X3 = 1
        if self.pub_S == 1: self.ZSX3_S = 1
        if self.pub_SF == 1 and self.Ele == 1:
            self.ZSX3_SF = 1
            self.Text.setText(f"X3-SF路径已建立: X3={self.pub_X3}, SF={self.pub_SF}, Ele={self.Ele}")
        else:
            self.Text.setText(f"X3按钮已按下: X3={self.pub_X3}, SF={self.pub_SF}, Ele={self.Ele}")
        self.Text.adjustSize(); self.Text.show()
        self.update()
        
    def Btn_XII(self):
        self.pub_XII = 1
        if self.pub_S == 1 and self.Ele == 1:
            self.ZSXII_S = 1
            self.Text.setText(f"XII-S路径已建立: XII={self.pub_XII}, S={self.pub_S}, Ele={self.Ele}")
        else:
            self.Text.setText(f"XII-S路径条件不满足: XII={self.pub_XII}, S={self.pub_S}, Ele={self.Ele}")
        self.Text.adjustSize(); self.Text.show()
        self.update()
    
    def Btn_XF(self):
        self.pub_XF = 1
        if self.pub_SII == 1 and self.Ele == 1 and self.pub_S4 == 0:
            self.ZSXF_SII = 1
            self.ZSXF_S4 = 0
            self.Text.setText(f"XF-SII路径已建立: XF={self.pub_XF}, SII={self.pub_SII}, Ele={self.Ele}")
        elif self.pub_S4 == 1 and self.Ele == 1 and self.pub_SII == 0:
            self.ZSXF_S4 = 1
            self.ZSXF_SII = 0
            self.Text.setText(f"XF-S4路径已建立: XF={self.pub_XF}, S4={self.pub_S4}, Ele={self.Ele}")
        else:
            self.Text.setText(f"XF按钮已按下: XF={self.pub_XF}, SII={self.pub_SII}, S4={self.pub_S4}, Ele={self.Ele}")
        self.Text.adjustSize(); self.Text.show()
        self.update()
    def Btn_X4(self):
        self.pub_X4 = 1
        if self.pub_S == 1 and self.Ele == 1:
            self.ZSX4_S = 1
            self.Text.setText(f"X4-S路径已建立: X4={self.pub_X4}, S={self.pub_S}, Ele={self.Ele}")
        else:
            self.Text.setText(f"X4-S路径条件不满足: X4={self.pub_X4}, S={self.pub_S}, Ele={self.Ele}")
        self.Text.adjustSize(); self.Text.show()
        self.update()
    
    def Btn_S(self):
        self.pub_S = 1
        if self.pub_XII == 1 and self.Ele == 1: 
            self.ZSXII_S = 1
            self.Text.setText(f"XII-S路径已建立: XII={self.pub_XII}, S={self.pub_S}, Ele={self.Ele}")
        elif self.pub_X4 == 1 and self.Ele == 1:
            self.ZSX4_S = 1
            self.Text.setText(f"X4-S路径已建立: X4={self.pub_X4}, S={self.pub_S}, Ele={self.Ele}")
        else:
            self.Text.setText(f"S按钮已按下: XII={self.pub_XII}, X4={self.pub_X4}, S={self.pub_S}, Ele={self.Ele}")
        self.Text.adjustSize(); self.Text.show()
        self.update()
    
    def Btn_D1(self):
        self.pub_D1 = 1
        if self.pub_D3 == 1: self.ZSD1D3 = 1
        self.update()
    
    def Btn_D3(self):
        self.pub_D3 = 1
        if self.pub_D1 == 1: self.ZSD1D3 = 1
        if self.pub_D7 == 1: self.ZSD3D7 = 1
        self.update()
    
    def Btn_D7(self):
        self.pub_D7 = 1
        if self.pub_D3 == 1: self.ZSD3D7 = 1
        if self.pub_SII == 1: self.ZSD7SII = 1
        if self.pub_S4 == 1: self.ZSD7S4 = 1
        self.update()

    def mousePressEvent(self, event): self.mouseX, self.mouseY = event.x(), event.y()
    def mouseMoveEvent(self, event): self.mouseX, self.mouseY = event.x(), event.y()

    def Time_Display(self):
        t = QDateTime.currentDateTime().toString("yyyy-MM-dd\nhh:mm:ss\ndddd")
        self.Tc.setText(t); self.Tc.adjustSize(); self.Tc.move(1760,25)

    def Time_Display(self):
        t = QDateTime.currentDateTime().toString("yyyy-MM-dd\nhh:mm:ss\ndddd")
        self.Tc.setText(t); self.Tc.adjustSize(); self.Tc.move(1760,25)

    def draw_switches(self, p):
        # 1/3道岔绘制
        p.setPen(QPen(Qt.black, 4))
        dc_1_3_shape = [QPointF(239, 453), QPointF(249, 453), QPointF(327, 342), QPointF(337, 342)] if self.dc_1_3_pos == 2 else [QPointF(239, 449), QPointF(249, 449), QPointF(327, 346), QPointF(337, 346)]
        p.drawPolyline(QPolygonF(dc_1_3_shape))
        if self.dc_1_3_locked: p.setPen(QPen(Qt.yellow, 6)); p.drawPolyline(QPolygonF(dc_1_3_shape))

        # 5/7道岔绘制 (Crossover)
        p.setPen(QPen(Qt.black, 4))
        if self.dc_5_7_pos == 2: # 反位
            dc_5_7_shape = [QPointF(385, 346), QPointF(395, 346), QPointF(481, 449), QPointF(491, 449)]
            p.drawPolyline(QPolygonF(dc_5_7_shape))
            if self.dc_5_7_locked: p.setPen(QPen(Qt.yellow, 6)); p.drawPolyline(QPolygonF(dc_5_7_shape))
        else: # 定位
            p.drawLine(385, 342, 491, 342)
            p.drawLine(385, 453, 491, 453)
            if self.dc_5_7_locked: p.setPen(QPen(Qt.yellow, 6)); p.drawLine(385, 342, 491, 342); p.drawLine(385, 453, 491, 453)

        # 9道岔绘制 (Turnout)
        p.setPen(QPen(Qt.black, 4))
        dc_9_shape = [QPointF(519, 338), QPointF(529, 338), QPointF(625, 218)] if self.dc_9_pos == 2 else [QPointF(519, 342), QPointF(625, 342)]
        p.drawPolyline(QPolygonF(dc_9_shape))
        if self.dc_9_locked: p.setPen(QPen(Qt.yellow, 6)); p.drawPolyline(QPolygonF(dc_9_shape))

        # 11道岔绘制 (Turnout)
        p.setPen(QPen(Qt.black, 4))
        dc_11_shape = [QPointF(516, 457), QPointF(526, 457), QPointF(623, 555)] if self.dc_11_pos == 2 else [QPointF(516, 453), QPointF(623, 453)]
        p.drawPolyline(QPolygonF(dc_11_shape))
        if self.dc_11_locked: p.setPen(QPen(Qt.yellow, 6)); p.drawPolyline(QPolygonF(dc_11_shape))



    def paintEvent(self, event):
        p = QPainter(self)
        p.setRenderHint(QPainter.Antialiasing)

        p.setPen(QPen(Qt.black,4))
        p.drawLine(625,218,1056,218); p.drawLine(29,342,1864,342)
        p.drawLine(28,453,1869,453); p.drawLine(623,555,1132,555)
        # 动态绘制道岔
        self.draw_switches(p)

        p.drawPolyline(QPolygonF([QPointF(385, 346), QPointF(395, 346), QPointF(481, 449), QPointF(491, 449)]))
        p.drawPolyline(QPolygonF([QPointF(519, 338), QPointF(529, 338), QPointF(625, 218)]))
        p.drawPolyline(QPolygonF([QPointF(516, 457), QPointF(526, 457), QPointF(623, 555)]))
        p.drawPolyline(QPolygonF([QPointF(1516, 449), QPointF(1526, 449), QPointF(1655, 346), QPointF(1665, 346)]))
        p.drawPolyline(QPolygonF([QPointF(1327, 346), QPointF(1337, 346), QPointF(1442, 449), QPointF(1452, 449)]))
        p.drawPolyline(QPolygonF([QPointF(1274, 457), QPointF(1264, 457), QPointF(1132, 555)]))
        p.drawPolyline(QPolygonF([QPointF(1173, 338), QPointF(1163, 338), QPointF(1056, 218)]))

        p.setPen(QPen(Qt.black, 4))
        dc_shape = [QPointF(239, 453), QPointF(249, 453), QPointF(327, 342), QPointF(337, 342)] if self.dc_1_3_pos == 2 else [QPointF(239, 449), QPointF(249, 449), QPointF(327, 346), QPointF(337, 346)]
        p.drawPolyline(QPolygonF(dc_shape))
        if self.dc_1_3_locked: p.setPen(QPen(Qt.yellow, 6)); p.drawPolyline(QPolygonF(dc_shape))

        p.setPen(Qt.black); p.setFont(QFont("宋体",10,QFont.Bold))
        p.drawText(823,324,"IG"); p.drawText(831,434,"IIG"); p.drawText(834,543,"4G"); p.drawText(817,205,"3G")

        p.setPen(QPen(Qt.black, 2))
        p.drawLine(678,213,678,223);p.drawLine(985,213,985,223);p.drawLine(65,335,65,345);p.drawLine(361,335,361,345);p.drawLine(1258,335,1258,345);p.drawLine(1512,335,1512,345);p.drawLine(1688,335,1688,345);p.drawLine(1799,335,1799,345);p.drawLine(645,335,645,345);p.drawLine(1000,335,1000,345);p.drawLine(66,447,66,457);p.drawLine(181,447,181,457);p.drawLine(325,447,325,457);p.drawLine(502,447,502,457);p.drawLine(1487,447,1487,457);p.drawLine(1789,447,1789,457);p.drawLine(643,447,643,457);p.drawLine(1036,447,1036,457);p.drawLine(670,550,670,560);p.drawLine(1031,550,1031,560)
        p.setPen(QPen(Qt.black,3));p.setBrush(QBrush(Qt.black))
        p.drawLine(678,228,678,248);p.drawLine(985,185,985,205);p.drawLine(65,310,65,330);p.drawLine(361,310,361,330);p.drawLine(645,350,645,370);p.drawLine(1000,310,1000,330);p.drawLine(1258,350,1258,370);p.drawLine(1512,310,1512,330);p.drawLine(1688,350,1688,370);p.drawLine(1799,310,1799,330);p.drawLine(65,462,65,482);p.drawLine(181,422,181,442);p.drawLine(325,462,325,482);p.drawLine(502,422,502,442);p.drawLine(643,462,643,482);p.drawLine(1036,422,1036,442);p.drawLine(1487,462,1487,482);p.drawLine(1789,462,1789,482);p.drawLine(670,565,670,585);p.drawLine(1031,525,1031,545)
        p.setPen(Qt.black)

        p.setBrush(Qt.blue if self.Ele==1 else Qt.black)
        p.drawEllipse(183,422,15,15);p.drawEllipse(307,466,15,15);p.drawEllipse(363,310,15,15);p.drawEllipse(503,423,15,15);p.drawEllipse(1670,355,15,15);p.drawEllipse(1515,310,15,15);p.drawEllipse(1469,466,15,15);p.drawEllipse(1240,354,15,15)
        p.setBrush(Qt.red if self.Ele==1 else Qt.black)
        p.drawEllipse(660,233,15,15);p.drawEllipse(987,186,15,15);p.drawEllipse(627,354,15,15);p.drawEllipse(626,465,15,15);p.drawEllipse(1037,423,15,15);p.drawEllipse(652,567,15,15);p.drawEllipse(1033,525,15,15);p.drawEllipse(68,465,15,15);p.drawEllipse(1771,467,15,15);p.drawEllipse(1781,312,15,15)

        # 绘制信号灯
        if self.Ele == 1:
            # X信号灯
            if self.ZSXSI == 1 or self.ZSXS3 == 1:
                p.setBrush(Qt.yellow)
            else:
                p.setBrush(Qt.red)
            p.drawEllipse(68,312,15,15)

            # XI信号灯 (已移除)
            p.setBrush(Qt.red)
            p.drawEllipse(1002,312,15,15)

            # XF信号灯
            if hasattr(self, 'xf_signal_color'):
                p.setBrush(Qt.yellow if self.xf_signal_color == "yellow" else Qt.red)
            else:
                p.setBrush(Qt.red)
            p.drawEllipse(26, 456, 15, 15)

            # X3信号灯
            if hasattr(self, 'x3_signal_color'):
                p.setBrush(Qt.yellow if self.x3_signal_color == "yellow" else Qt.red)
            else:
                p.setBrush(Qt.red)
            p.drawEllipse(947, 196, 15, 15)

            # XII信号灯
            if hasattr(self, 'xii_signal_color'):
                p.setBrush(Qt.yellow if self.xii_signal_color == "yellow" else Qt.red)
            else:
                p.setBrush(Qt.red)
            p.drawEllipse(995, 411, 15, 15)

            # X4信号灯
            if hasattr(self, 'x4_signal_color'):
                p.setBrush(Qt.yellow if self.x4_signal_color == "yellow" else Qt.red)
            else:
                p.setBrush(Qt.red)
            p.drawEllipse(991, 513, 15, 15)

        p.setPen(Qt.black); p.setBrush(Qt.green if self.Ele==1 else Qt.black)
        Elp_x1=282;Elp_x2=1408;Elp_y=70;Elp_r=20
        for _ in range(4):p.drawEllipse(Elp_x1,Elp_y,Elp_r,Elp_r);Elp_x1+=75
        for _ in range(4):p.drawEllipse(Elp_x2,Elp_y,Elp_r,Elp_r);Elp_x2+=75

        # --- X->S3 Route Display Logic ---
        if self.ZSXS3 == 1:
            # Segment 1: Bottom straight
            pen_color = Qt.green
            if self.XS3section_state == 1: pen_color = Qt.red
            elif self.XS3section_state > 1: pen_color = Qt.black
            p.setPen(QPen(pen_color, 4)); p.drawLine(65, 342, 532, 342)
            # Segment 2: Curve
            pen_color = Qt.green
            if self.XS3section_state == 2: pen_color = Qt.red
            elif self.XS3section_state > 2: pen_color = Qt.black
            p.setPen(QPen(pen_color, 4)); p.drawPolyline(QPolygonF([QPointF(519, 338), QPointF(529, 338), QPointF(625, 218)]))
            # Segment 3: Top straight
            pen_color = Qt.green
            if self.XS3section_state >= 3: pen_color = Qt.red
            elif self.XS3section_state > 3: pen_color = Qt.black
            p.setPen(QPen(pen_color, 4)); p.drawLine(625, 218, 985, 218)
            # Gap eraser
            p.setPen(QPen(QColor("#F2F2F2"), 6)); p.drawLine(532, 342, 549, 342)

        if self.ZSXSI == 1: p.setPen(QPen(Qt.green, 4)); p.drawLine(65, 342, 1000, 342)
        if self.ZSD5SI == 1: p.setPen(QPen(Qt.green, 6)); p.drawLine(361, 342, 645, 342)

        if self.ZSXI_SF == 1:
            if self.XISFsection_state < 1:p.setPen(QPen(Qt.green,4));p.drawLine(1010,342,1200,342)
            if self.XISFsection_state < 2:p.setPen(QPen(Qt.green,4));p.drawLine(1200,342,1600,342)
            if self.XISFsection_state < 3:p.setPen(QPen(Qt.green,4));p.drawLine(1600,342,1820,342)
        if self.XISFsection_state==1:p.setPen(QPen(Qt.red,4));p.drawLine(1010,342,1200,342)
        elif self.XISFsection_state==2:p.setPen(QPen(Qt.red,4));p.drawLine(1200,342,1600,342)
        elif self.XISFsection_state==3:p.setPen(QPen(Qt.red,4));p.drawLine(1600,342,1820,342)

        def draw_seg(x1,x2,y,color):p.setPen(QPen(color,4));p.drawLine(x1,y,x2,y)
        if hasattr(self,'D5SIsection') and self.D5SIsection==1:draw_seg(363,627,342,Qt.red)
        if self.section==1:draw_seg(65,361,342,Qt.red);p.setBrush(QBrush(Qt.red));p.drawEllipse(68,312,15,15)
        if self.section1==2:draw_seg(361,645,342,Qt.red);draw_seg(65,361,342,Qt.black)
        if self.section2==3:draw_seg(645,1000,342,Qt.red);draw_seg(361,645,342,Qt.black)
        if self.section3==4:draw_seg(645,1000,342,Qt.black);QTimer.singleShot(500,self.XC_XSIqing)

        # --- XF->SII Route Display Logic ---
        if self.ZSXF_SII == 1 and self.Ele == 1:
            if hasattr(self, 'XFSII_section_state'):
                # 区段1
                color = Qt.red if self.XFSII_section_state >= 1 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(26, 453, 141, 453)
                # 区段2
                color = Qt.red if self.XFSII_section_state >= 2 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(141, 453, 329, 453)
                # 区段3
                color = Qt.red if self.XFSII_section_state >= 3 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(329, 453, 473, 453)
                # 区段4
                color = Qt.red if self.XFSII_section_state >= 4 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(473, 453, 648, 453)
            else:
                p.setPen(QPen(Qt.green, 4))
                p.drawLine(26, 453, 141, 453)
                p.drawLine(141, 453, 329, 453)
                p.drawLine(329, 453, 473, 453)
                p.drawLine(473, 453, 648, 453)

        # --- XF->S4 Route Display Logic ---
        if self.ZSXF_S4 == 1:
            if hasattr(self, 'XFS4_section_state'):
                # 区段1
                color = Qt.red if self.XFS4_section_state >= 1 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(26, 453, 141, 453)
                # 区段2
                color = Qt.red if self.XFS4_section_state >= 2 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(141, 453, 329, 453)
                # 区段3
                color = Qt.red if self.XFS4_section_state >= 3 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(329, 453, 473, 453)
                # 区段4
                color = Qt.red if self.XFS4_section_state >= 4 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(473, 453), QPointF(516, 457), QPointF(623, 555), QPointF(673, 555)]))
            else:
                p.setPen(QPen(Qt.green, 4))
                p.drawLine(26, 453, 141, 453)
                p.drawLine(141, 453, 329, 453)
                p.drawLine(329, 453, 473, 453)
                p.drawPolyline(QPolygonF([QPointF(473, 453), QPointF(516, 457), QPointF(623, 555), QPointF(673, 555)]))

        # --- XII->S Route Display Logic ---
        if self.ZSXII_S == 1:
            if hasattr(self, 'XIIS_section_state'):
                # 区段1
                color = Qt.red if self.XIIS_section_state >= 1 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(995, 453, 1492, 453)
                # 区段2
                color = Qt.red if self.XIIS_section_state >= 2 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(1492, 453, 1797, 453)
            else:
                p.setPen(QPen(Qt.green, 4))
                p.drawLine(995, 453, 1492, 453)
                p.drawLine(1492, 453, 1797, 453)

        # --- X4->S Route Display Logic ---
        if self.ZSX4_S == 1:
            if hasattr(self, 'X4S_section_state'):
                # 区段1
                color = Qt.red if self.X4S_section_state >= 1 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(991, 553), QPointF(1140, 555)]))
                # 区段2
                color = Qt.red if self.X4S_section_state >= 2 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(1140, 555), QPointF(1280, 453)]))
                # 区段3
                color = Qt.red if self.X4S_section_state >= 3 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(1280, 453), QPointF(1492, 453)]))
                # 区段4
                color = Qt.red if self.X4S_section_state >= 4 else Qt.green
                p.setPen(QPen(color, 4)); p.drawLine(1492, 453, 1797, 453)
            else:
                p.setPen(QPen(Qt.green, 4))
                p.drawPolyline(QPolygonF([QPointF(991, 553), QPointF(1140, 555), QPointF(1280, 453), QPointF(1492, 453)]))
                p.drawLine(1492, 453, 1797, 453)

        # --- X3->SF Route Display Logic ---
        if self.ZSX3_SF == 1:
            if hasattr(self, 'X3SF_section_state'):
                # 区段1
                color = Qt.red if self.X3SF_section_state >= 1 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(947, 206), QPointF(1040, 206)]))
                # 区段2
                color = Qt.red if self.X3SF_section_state >= 2 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(1040, 206), QPointF(1190, 345)]))
                # 区段3
                color = Qt.red if self.X3SF_section_state >= 3 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(1190, 345), QPointF(1263, 345)]))
                # 区段4
                color = Qt.red if self.X3SF_section_state >= 4 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(1263, 345), QPointF(1471, 345)]))
                # 区段5
                color = Qt.red if self.X3SF_section_state >= 5 else Qt.green
                p.setPen(QPen(color, 4)); p.drawPolyline(QPolygonF([QPointF(1471, 345), QPointF(1695, 345), QPointF(1804, 345)]))
            else:
                p.setPen(QPen(Qt.green, 4))
                p.drawPolyline(QPolygonF([
                    QPointF(947, 206),  # X3
                    QPointF(1040, 206),  # 过渡点1
                    QPointF(1190, 345),  # 过渡点3
                    QPointF(1263, 345),  # D8
                    QPointF(1471, 345),  # D4
                    QPointF(1695, 345),  # D2
                    QPointF(1804, 345)   # SF
                ]))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Me()
    win.show()
    sys.exit(app.exec_())